﻿Imports Microsoft.VisualBasic

Namespace MyApp
    Namespace Network

        Public Class Instagram
            Private db As mDB = mDB.GetDB

            Public Property oauth2 As WebApi.Oauth.Oauth2

            Public Function RedirectAuth() As String

                Return oauth2.GetDialog("https://api.instagram.com/oauth/authorize", "basic")

            End Function

            Public Sub AddLogin(code As String, access_token As String)
                Dim db = mDB.GetDB
                Dim CurrUser = MyApp.GetUser(False)

                'L'utente riconosciuto da Twitter ci ha autorizzato, chiedo quindi a Twitter i dati dell'utente
                oauth2.access_token = access_token
                oauth2.code = code

                If access_token = "" Then
                    oauth2.ConfirmingIdentity("https://api.instagram.com/oauth/access_token", "POST", "JSON")
                End If

                Dim tc = New WebApi.Instagram.InstagramClient(oauth2)

                Dim tu = tc.GetCurrUser

                '--------------------------------------------
                ' creo account

                Dim acc = Me.UpdateAccount(tu)

                '--------------------------------------------
                ' creo account_update
                Dim acc_update = acc.Account_Update

                '--------------------------------------------
                ' creo account_auth


                Dim AccAuth = acc.Account_Auth ' CurrUser.tab_acc Tab_UserAccount.BY.User_Account.Account_Id(acc.Id).Limit(1).ToObj
                If AccAuth Is Nothing Then
                    AccAuth = New mDB.Account_Auth
                    db.TAB.Account_Auth.InsertOnSubmit(AccAuth)
                End If

                AccAuth.Account_Id = acc.Id
                AccAuth.Network_Id = MyApp.Network.AccType.INSTAGRAM_ACCOUNT
                AccAuth.Oauth_Consumer_Key = oauth2.client_id
                AccAuth.Oauth_Consumer_Secret = oauth2.client_secret
                AccAuth.Oauth_Token = oauth2.access_token
                AccAuth.Oauth_Token_Secret = oauth2.refresh_token

                AccAuth.Error_Account_Last_Date = Date.MinValue
                AccAuth.Error_Account_Count = 0
                AccAuth.Disabled = 0


                '--------------------------------------------
                ' creo user_account

                Dim UserAcc = CurrUser.Tab_User_Account.BY.User_Account.Account_Id(acc.Id).Limit(1).ToObj

                If UserAcc Is Nothing Then
                    UserAcc = New mDB.User_Account
                    db.TAB.User_Account.InsertOnSubmit(UserAcc)
                End If

                UserAcc.Account_Id = acc.Id
                UserAcc.User_Id = CurrUser.Id

                db.SubmitChanges()

            End Sub


            Public Function UpdateAccount(tu As WebApi.Instagram.User) As mDB.Account

                '--------------------------------------------
                ' creo account
                Dim Acc = mDB.Account.GetForUpdate(tu.id, AccType.INSTAGRAM_ACCOUNT)


                Acc.Fullname = tu.full_name
                Acc.Username = tu.username
                Acc.Avatar = tu.profile_picture
                Acc.Description = tu.bio
                Acc.Url = tu.website

                Acc.Account_Add_Info.Profile_Update = Now
                Acc.Account_Add_Info.Follower_Count = tu.counts.followed_by
                Acc.Account_Add_Info.Friend_Count = tu.counts.follows
                Acc.Account_Add_Info.Post_Count = tu.counts.media

                db.SubmitChanges()

                Return Acc

            End Function


        End Class
    End Namespace
End Namespace
